#include "preferencesdialog.h"

PreferencesDialog::PreferencesDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle("Preferences");

    loadPreferences();

    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);

    QVBoxLayout *topLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);

    QHBoxLayout *textLayout = new QHBoxLayout;
    topLayout->addLayout(textLayout);

    QLabel *textLabel = new QLabel("Text Color");
    textLayout->addWidget(textLabel);

    QPixmap pixmap(25,25);
    pixmap.fill(textColor);

    textSquare = new ColorSquare;
    textSquare->setPixmap(pixmap);
    textLayout->addWidget(textSquare);

    QHBoxLayout *backLayout = new QHBoxLayout;
    topLayout->addLayout(backLayout);

    QLabel *backLabel = new QLabel("Back color");
    backLayout->addWidget(backLabel);

    pixmap.fill(backColor);

    backSquare = new ColorSquare;
    backSquare->setPixmap(pixmap);
    backLayout->addWidget(backSquare);


    QHBoxLayout *dobackLayout = new QHBoxLayout;
    topLayout->addLayout(dobackLayout);

    QLabel *dobackLabel = new QLabel("Draw label background");
    dobackLayout->addWidget(dobackLabel);

    QCheckBox *dobackCheck = new QCheckBox;
    dobackCheck->setChecked(drawBack);
    dobackLayout->addWidget(dobackCheck);


    QHBoxLayout *startLayout = new QHBoxLayout;
    topLayout->addLayout(startLayout);

    QLabel *startLabel = new QLabel("Start counting at");
    startLayout->addWidget(startLabel);

    QRadioButton *start0Radio = new QRadioButton("0");
    startLayout->addWidget(start0Radio);

    QRadioButton *start1Radio = new QRadioButton("1");
    startLayout->addWidget(start1Radio);

    if(startAt==0){
        start0Radio->setChecked(true);
    }else{
        start1Radio->setChecked(true);
    }

    QHBoxLayout *showcountLayout = new QHBoxLayout;
    topLayout->addLayout(showcountLayout);

    QLabel *showcountLabel = new QLabel("Show chromosome count");
    showcountLayout->addWidget(showcountLabel);

    QCheckBox *showcountCheck = new QCheckBox;
    showcountCheck->setChecked(showCount);
    showcountLayout->addWidget(showcountCheck);

    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    mainLayout->addLayout(buttonsLayout);

    QPushButton *okButton = new QPushButton("Ok");
    QPushButton *cancelButton = new QPushButton("Cancel");

    buttonsLayout->addWidget(okButton);
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->setAlignment(Qt::AlignRight);

    connect(textSquare, SIGNAL(clicked()),
            this, SLOT(textColorPick()));

    connect(backSquare, SIGNAL(clicked()),
            this, SLOT(backColorPick()));

    connect(start1Radio, SIGNAL(toggled(bool)),
            this, SLOT(setStartAt(bool)));

    connect(dobackCheck, SIGNAL(toggled(bool)),
            this, SLOT(setDrawBack(bool)));

    connect(showcountCheck, SIGNAL(toggled(bool)),
            this, SLOT(setShowCount(bool)));

    connect(okButton,SIGNAL(clicked()),this,SLOT(savePreferences()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));

}

void PreferencesDialog::textColorPick(){
    QColor newColor = QColorDialog::getColor(textColor,this);

    if(!newColor.isValid())
        return;

    textColor = newColor;

    QPixmap pixmap(25,25);
    pixmap.fill(textColor);

    textSquare->setPixmap(pixmap);
}

void PreferencesDialog::backColorPick(){
    QColor newColor = QColorDialog::getColor(backColor,this);

    if(!newColor.isValid())
        return;

    backColor = newColor;

    QPixmap pixmap(25,25);
    pixmap.fill(backColor);

    backSquare->setPixmap(pixmap);
}

void PreferencesDialog::loadPreferences(){
    QSettings settings("TID","Cromos");
    int r = settings.value("textR",0).toInt();
    int g = settings.value("textG",0).toInt();
    int b = settings.value("textB",0).toInt();

    textColor.setRgb(r,g,b);

    r = settings.value("backR",160).toInt();
    g = settings.value("backG",160).toInt();
    b = settings.value("backB",160).toInt();

    backColor.setRgb(r,g,b);

    startAt = settings.value("start_at",0).toInt();
    drawBack = settings.value("draw_back",true).toBool();
    showCount = settings.value("show_count",true).toBool();

}

void PreferencesDialog::savePreferences(){

    QSettings settings("TID","Cromos");
    settings.setValue("textR",textColor.red());
    settings.setValue("textG",textColor.green());
    settings.setValue("textB",textColor.blue());


    settings.setValue("backR",backColor.red());
    settings.setValue("backG",backColor.green());
    settings.setValue("backB",backColor.blue());


    settings.setValue("start_at",startAt);
    settings.setValue("draw_back",drawBack);
    settings.setValue("show_count",showCount);

    accept();
}
